package com.sakiko.audiobook.common.login;

import com.sakiko.audiobook.common.constant.RedisConstant;
import com.sakiko.audiobook.common.execption.GuiguException;
import com.sakiko.audiobook.common.execption.SakiException;
import com.sakiko.audiobook.common.result.ResultCodeEnum;
import com.sakiko.audiobook.common.util.AuthContextHolder;
import com.sakiko.audiobook.model.user.UserInfo;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

/**
 * projectName: com.sakiko.audiobook.common.login
 *
 * @author: sakiko
 * time: 2025/9/9 18:20
 * description: 主要实现的业务: 判断用户是否需要登陆，需要登陆，没有登陆的话，则跳转到登录页面！！！
 * 向前端抛出异常: ResultCodeEnum.LOGIN_AUTH
 * 前端根据提示信息跳转登陆页面
 */
@Component
@Aspect
@Slf4j
public class SakiLoginAspect {

    @Autowired
    private RedisTemplate redisTemplate;

    @Around("@annotation(sakiLogin)") // 判断用户是否需要登陆，可以同时搭配使用 pointcut指定生效位置
    public Object SakiAspect(ProceedingJoinPoint point, SakiLogin sakiLogin) throws Throwable {
        // 登陆完成之后: 1、返回一个 token存储到请求头中 2、将用户信息存储到 redis中
        // 获取请求对象
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) requestAttributes;
        HttpServletRequest request = servletRequestAttributes.getRequest();
//        HttpServletResponse response = servletRequestAttributes.getResponse();
        // 获取请求头中的 token
        String token = request.getHeader("token");
        // 必须登陆部分 逻辑
        if (sakiLogin.required()) {
            // token is null
            if (!StringUtils.hasText(token)) {
                throw new SakiException(ResultCodeEnum.LOGIN_AUTH);
            } else {
                // 缓存中获取用户信息
                String loginKey = RedisConstant.USER_LOGIN_KEY_PREFIX + token;
                UserInfo userInfo = (UserInfo) redisTemplate.opsForValue().get(loginKey);
                if (null == userInfo) {
                    // 缓存中没有用户信息
                    throw new SakiException(ResultCodeEnum.LOGIN_AUTH);
                }
            }
        }
        // 不需要登陆
        try {
            if (StringUtils.hasText(token)) {
                String loginKey = RedisConstant.USER_LOGIN_KEY_PREFIX + token;
                UserInfo userInfo = (UserInfo) redisTemplate.opsForValue().get(loginKey);
                if (null != userInfo) {
                    AuthContextHolder.setUserId(userInfo.getId());
                }
            }
            // 执行带有注解的方法体; 返回数据
            return point.proceed();
        } finally {
            // 删除本地线程中的数据
            AuthContextHolder.removeUserId();
        }
    }

}
